package com.anbow.courseware.service.impl;

import com.anbow.courseware.entity.DO.VipTeachingQuestions;
import com.anbow.courseware.entity.DTO.VipTeachingQuestionsDTO;
import com.anbow.courseware.entity.FORM.VipTeachingQuestionsForm;
import com.anbow.courseware.mapper.VipTeachingQuestionsMapper;
import com.anbow.courseware.service.VipTeachingQuestionsService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhaoYuFei
 */
@Service
public class VipTeachingQuestionsServiceImpl implements VipTeachingQuestionsService {
    @Autowired
    private VipTeachingQuestionsMapper vipTeachingQuestionsMapper;

    @Override
    public List<VipTeachingQuestionsDTO> getAll() {
        List<VipTeachingQuestions> all = vipTeachingQuestionsMapper.getAll();
        List<VipTeachingQuestionsDTO> collect = all.stream().map(e -> {
            VipTeachingQuestionsDTO vipTeachingQuestionsDTO = new VipTeachingQuestionsDTO();
            BeanUtils.copyProperties(e, vipTeachingQuestionsDTO);
            return vipTeachingQuestionsDTO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<VipTeachingQuestionsDTO> getLabel(String label,Integer pageNum) {
        if (pageNum==null||"null".equals(pageNum)){
            pageNum=0;
        }else {
           pageNum= (pageNum-1)*10;
        }
        List<VipTeachingQuestions> list = vipTeachingQuestionsMapper.getLabel(label,pageNum);
        List<VipTeachingQuestionsDTO> collect = list.stream().map(e -> {
            VipTeachingQuestionsDTO vipTeachingQuestions = new VipTeachingQuestionsDTO();
            BeanUtils.copyProperties(e, vipTeachingQuestions);
            return vipTeachingQuestions;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public VipTeachingQuestionsDTO getById(String id) {
      VipTeachingQuestions vipTeachingQuestions=  vipTeachingQuestionsMapper.getById(id);
      VipTeachingQuestionsDTO vipTeachingQuestionsDTO=new VipTeachingQuestionsDTO();
      BeanUtils.copyProperties(vipTeachingQuestions,vipTeachingQuestionsDTO);
        return vipTeachingQuestionsDTO;
    }

    @Override
    public void deleteById(String id) {
        vipTeachingQuestionsMapper.delete(id);
    }

    @Override
    public void update(VipTeachingQuestionsForm vipTeachingQuestionsForm) {
        vipTeachingQuestionsMapper.update(vipTeachingQuestionsForm);
    }

    @Override
    public void add(VipTeachingQuestionsDTO vipTeachingQuestionsDTO) {
        VipTeachingQuestions vipTeachingQuestions=new VipTeachingQuestions();
        BeanUtils.copyProperties(vipTeachingQuestionsDTO,vipTeachingQuestions);
        vipTeachingQuestionsMapper.add(vipTeachingQuestions);
    }

    @Override
    public StringBuilder getByIds(String ids) {
        String[] ss = ids.split(",");
        List<String> list = new ArrayList<>(Arrays.asList(ss));
        List<VipTeachingQuestions> byIds = vipTeachingQuestionsMapper.getByIds(list);
        StringBuilder result = new StringBuilder();
        String msg="";
        String titles="<h1 id=\"xfgrk\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " +
                msg +
                "</h1>";
        result.append(titles);
        for (int i = 0; i < byIds.size(); i++) {
            result.append("练习").append(i+1).append(":");
            result.append(byIds.get(i).getContext());
            result.append("答案：");
            result.append(byIds.get(i).getResult());
        }
        return result;
    }
}
